package com.telit.common.exception;


import com.telit.common.response.ResultCode;


/**
 * Create by Chencf on 2022/07/11
 * 基本异常类型
 * 所有自定义异常，都应该实现ResultCode接口，拦截器中会根据异常类型动态返回错误信息
 */
public class BaseException extends RuntimeException implements ResultCode {

    protected int code;
    protected String errorMsg;


    public BaseException(ResultCode resultCode){
        super(resultCode.getErrorMsg());
        if (resultCode != null) {
            this.code = resultCode.getCode();
            this.errorMsg = resultCode.getErrorMsg();
        }
    }

    public BaseException(ResultCode resultCode, String errorMsg){
        super(errorMsg);
        if (resultCode != null) {
            this.code = resultCode.getCode();
            this.errorMsg = errorMsg;
        }
    }

    @Override
    public int getCode() {
        return this.code;
    }

    @Override
    public String getErrorMsg() {
        return this.errorMsg;
    }

    /**
     * 自定义异常将不再爬取堆栈，提高异常性能
     */
    @Override
    public synchronized Throwable fillInStackTrace() {
        return this;
    }
}
